package com.matt.controller.pad;

import com.matt.commons.dto.sso.AreaDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.domain.Classes;
import com.matt.foundation.DO.ChildrenCourseItemDO;
import com.matt.foundation.DO.ClassesDO;
import com.matt.foundation.constant.CommonConstant;
import com.matt.foundation.constant.PermissionConstant;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.annotation.RequiredPermissions;
import com.matt.foundation.vo.ChildrenCheckRecordVo;
import com.matt.foundation.vo.ClassesCheckRecordVo;
import com.matt.foundation.vo.course.CoursePlanEarlyListVo;
import com.matt.repository.base.ChildChildrenCourseItemRepository;
import com.matt.service.ChildrenService;
import com.matt.service.ClassesService;
import com.matt.service.CourseEarlyService;
import com.matt.service.TargetObjectService;
import com.matt.service.remoteCall.SsoCallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

import static com.matt.foundation.constant.RequestConstant.TICKET;

;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/8/4
 * *******************************
 */
@RestController
@RequestMapping("/pad/home")
public class PadHomeController {

    @Autowired
    private ChildrenService childrenService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private ClassesService classesService;

    @Autowired
    private TargetObjectService targetObjectService;

    
    @Autowired
    private CourseEarlyService courseEarlyService;
    
    @Autowired
    private ChildChildrenCourseItemRepository childChildrenCourseItemRepository;

    @RequestMapping(value = "/area_base_info", method = RequestMethod.POST)
    public StatusResponse areaBaseInfo(@RequestHeader(TICKET) String ticket){
        Map response = new HashMap<>();

        AreaDTO  area = ssoCallService.getAreaDTO(ticket);

        List<ClassesDO> classesDOList = new ArrayList<>();
        List<Classes> classesList = classesService.getActiveClassesByArea(area.getId());
        int classesCount = classesList.size();
        for(Classes c : classesList){
            ClassesDO cdo = new ClassesDO();
            cdo.setClassesId(c.getId());
            cdo.setClassesName(c.getName());
            cdo.setLimitCount(c.getLimitNum());
            if(c.getCategory() ==1){
                cdo.setTargetName(targetObjectService.getById(c.getTargetObjectId()).getDescription());
            }
            cdo.setStudentCount(childrenService.getChildrenByClasses(c.getId()).size());
            classesDOList.add(cdo);
        }

        response.put("area", area);
        response.put("classesCount",classesCount);
        response.put("classesList",classesDOList);
        return StatusResponse.success(response);
    }

    @RequestMapping(value = "/getAreaStaffInfo", method = RequestMethod.POST)
    public StatusResponse getAreaStaffInfo(@RequestHeader(TICKET) String ticket){
        String areaId = cache.getAreaId(ticket) ;
        String platformId = CommonConstant.TEACH_PLATFORM_ID ;//托育智能芯


        //TODO:  这里需要变更
//        List<PlatformRoleDO> list = ssoCallService.getAreaStaffInfo(ticket, platformId, areaId);
//        List<String> staffIdList = ssoCallService.getNomarlStaffIdByArea(areaId);//未删除 未离职

        Map m = new HashMap<>();
//        m.put("staffRoleList",list);
//        m.put("staffCount",staffIdList.size());
        return StatusResponse.success(m);
    }

    @RequestMapping(value = "/check_record_by_classId", method = RequestMethod.POST)
    public StatusResponse checkRecordByClassId(@RequestHeader(TICKET) String ticket,String classId){
    	List<ClassesCheckRecordVo> recordList = childrenService.findClassesCheckRecordByClassId(classId);
    	List<ChildrenCheckRecordVo>  cc = new ArrayList<ChildrenCheckRecordVo>();
        recordList.forEach(r ->{
        	r.getChildrenList().forEach(c ->{
        		if(c.getCheckStatus() != 0 && null != c.getCheckTime()) {
        			cc.add(c);
        		}
        	});
        });
        Map map = new HashMap();
        map.put("total", recordList.get(0).getChildrenList().size());
        map.put("attendance", cc.size());
        return StatusResponse.success(map);
    }
    
    @RequestMapping(value = "/check_record_by_areaId", method = RequestMethod.POST)
    @RequiredPermissions(PermissionConstant.TEACH_CHECKRECORD_REPORT)
    public StatusResponse checkRecordByAreaId(@RequestHeader(TICKET) String ticket, String areaId){

        List<ChildrenCheckRecordVo> recordList = childrenService.findCheckRecordByAreaId(areaId);
        List<ChildrenCheckRecordVo>  cc = new ArrayList<ChildrenCheckRecordVo>();
        recordList.forEach(r ->{
            if(null != r.getCheckStatus() && r.getCheckStatus() != 0 && null != r.getCheckTime()) {
                cc.add(r);
            }
        });

        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        List<CoursePlanEarlyListVo> coursePlans = courseEarlyService.getPlanByAreaBetweenDays(dateFormat.format(new Date()), dateFormat.format(new Date()), areaId);
        List<ChildrenCourseItemDO> list = new ArrayList<ChildrenCourseItemDO>();
        coursePlans.forEach(c ->{
            List<ChildrenCourseItemDO> planedChildren = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(c.getCoursePlanClassesId());
            list.addAll(planedChildren);
        });

        Map map = new HashMap();
        map.put("total", recordList.size());
        map.put("attendance", cc.size());
        map.put("childrenNum", list.size());
        return StatusResponse.success(map);
    }
}
